package problem_everyday;

import java.util.Arrays;

public class D2025_06_30_P0594 {
    /*
    * 找出最长的和谐子序列（最大最小元素的差值为1）的长度
    * 1 <= nums.length <= 20000
    *   |nums[i]| <= 1e9
    * */

    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int res = 0;
        int last_num = -1000_000_000 - 2;
        int current_num = last_num;
        int last_num_count = 0;
        int current_num_count = 0;
        for(int i=0; i<nums.length; i++){
            if(nums[i]==current_num){
                current_num_count ++;
            } else {
                if(current_num == last_num + 1){
                    res = Math.max(current_num_count+last_num_count, res);
                }
                last_num = current_num;
                last_num_count = current_num_count;
                current_num = nums[i];
                current_num_count = 1;
            }
        }
        if(current_num == last_num + 1){
            res = Math.max(current_num_count+last_num_count, res);
        }
        return res;
    }


    public static void main(String[] args){
        D2025_06_30_P0594 p = new D2025_06_30_P0594();
        int[] nums = {1, 2, 2, 1};

    }
}
